// 培养方案导入 确认界面
<template>
  <div id="plan-import-confirm">
    <div class="assessment-container">
      <div class="table-container">
        <el-table
          :data="tableData"
          border
          height="calc(100vh - 350px)"
          style="width: 100%;"
          class="table-data"
          row-class-name="row-class"
          cell-class-name="cell-class"
          header-row-class-name="header-row-class"
          header-cell-class-name="header-cell-class"
          :span-method="objectSpanMethod"
          @selection-change="handleSelectionChange"
          :row-key="getRowKeys"
        >
          <el-table-column
            prop="pyLevel"
            label="培养层次"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="specialtyName"
            label="专业"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="year"
            label="年级"
            align="center"
          ></el-table-column>
          <el-table-column
            width="100"
            align="center"
            type="selection"
          ></el-table-column>
          <el-table-column
            prop="courseName"
            label="课程"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="xnxq"
            label="原安排学期"
            align="center"
          ></el-table-column>
        </el-table>
      </div>
    </div>

    <div class="wrapper-container">
      <div class="bottom-block" style="display: block">
        <div class="text">
          当前共找到{{ total }}条记录,当前显示{{ startPage }}-{{ endPage }}条
        </div>
      </div>
      <div class="pagination-block">
        <el-pagination
          prev-text="上一页"
          next-text="下一页"
          :page-size="pageSize"
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
        >
        </el-pagination>
      </div>
    </div>

    <div style="margin-top: 30px;text-align: center;">
      <el-button type="warning" plain @click="lastStep" size="mini" style="margin-right:150px">上一步</el-button>
      <el-button type="warning" plain @click="importBtn" size="mini" style="margin-right:150px">导 入</el-button>
      <el-button type="danger" plain @click="cancel" size="mini">取 消</el-button>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="importShowTip"
      width="800px"
      height="320px"
    >
      <div class="class-tip">确定从培养方案导入么？</div>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="warning" style="margin-right: 50px" size="mini" @click="tipConfirm"
          >确 定</el-button
        >
        <el-button plain type="danger" @click="importShowTip = false" size="mini"
          >取 消</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ButtonItem from "@/components/ButtonItem.vue";
import {
  queryPykcByFaId,
  addFromPykcByNdzyId,
} from "@/api/trainingManagement/place.js";
export default {
  components: { ButtonItem },
  data() {
    return {
      importShowTip: false,
      xq: "",
      xqList: [],
      tableData: [],
      pykcId: "",
      /*对象名称集*/
      parameterNameArr: [],
      pykcList: [], //选择课程列表
      selctList: [], //所选数据
      total: 0, //总条数
      pageSize: 50, //条数
      startPage: 0, //开始条数
      endPage: 0, //结束条数
      currentPage: 1, //当前页数
    };
  },
  methods: {
    getRowKeys(row) {
      return row.specialtyId
    },
    handleSelectionChange(val) {
      this.selctList = val;
    },
    // 取消
    cancel() {
      this.$router.push({
        path: "/admin/teachingTaskManage/termExecutionPlan",
      });
    },
    // 上一步
    lastStep() {
      this.$router.push({
        path: "/admin/teachingTaskManage/planImport",
      });
    },
    async tipConfirm() {
      if (this.selctList.length > 0) {
        var newArr = [];
        this.selctList.forEach((item, index) => {
          if (index == 0) {
            var par = {
              year: item.year,
              specialtyId: item.specialtyId,
              xueqi: item.xueqi,
              kcList: [],
            };
            var kcList = [{ courseId: item.courseId }];
            par.kcList = kcList;
            newArr.push(par);
          } else {
            var flag = false;
            newArr.forEach((dat, index) => {
              if (dat.specialtyId == item.specialtyId) {
                flag = true;
                var kcList = {
                  courseId: item.courseId,
                };
                newArr[index].kcList.push(kcList);
              }
            });
            if (!flag) {
              var par = {
                year: item.year,
                specialtyId: item.specialtyId,
                xueqi: item.xueqi,
                kcList: [],
              };
              var kcList = [
                {
                  courseId: item.courseId,
                },
              ];
              par.kcList = kcList;
              newArr.push(par);
            }
          }
        });
        const res = await addFromPykcByNdzyId(newArr);
        if (res.data.code == 200) {
          this.$message.success("导入成功!");
          this.importShowTip = false;
          this.$router.push("/admin/teachingTaskManage/termExecutionPlan");
        }
      } else {
        this.$message.warning("请先选择要导入的课程!");
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.queryInfo();
    },
    async queryInfo() {
      var current = this.currentPage;
      var size = this.pageSize;
      var params = this.pykcList;
      const res = await queryPykcByFaId(params, current, size);
      if (res.data.code == 200) {
        var data = [];
        if (res.data.data.records.length > 0) {
          res.data.data.records.forEach((item) => {
            if (item.kcList.length > 0) {
              item.kcList.forEach((da) => {
                let par = {
                  pyLevel: item.pyLevel,
                  year: item.year,
                  specialtyId: item.specialtyId,
                  specialtyName: item.specialtyName,
                  xueqi: item.xueqi,
                  courseId: da.courseId,
                  courseName: da.courseName,
                  xnxq: da.xnxq,
                  ndzyId: da.ndzyId,
                };
                data.push(par);
              });
            } else {
              let par = {
                pyLevel: item.pyLevel,
                year: item.year,
                specialtyId: item.specialtyId,
                specialtyName: item.specialtyName,
                xueqi: item.xueqi,
                courseId: "",
                courseName: "",
                xnxq: "",
                ndzyId: "",
              };
              data.push(par);
            }
          });
        }
        this.tableData = data;
        console.log(this.tableData, "table === ")
        this.total = res.data.data.total;
        var length = this.tableData.length;
        this.calculatePage(this.pageSize, res.data.data.current, length);
        this.getSpanArr(this.tableData);
      }
    },
    // 计算开始和结束条数
    calculatePage(pageSize, current, length) {
      if (current == 1) {
        if (length != 0) {
          this.startPage = 1;
          this.endPage = length;
        }
      } else {
        this.startPage = pageSize * (current - 1) + 1;
        this.endPage = parseInt(this.startPage) + parseInt(length) - 1;
      }
    },
    importBtn() {
      this.importShowTip = true;
    },
    /*获取相同数据进行合并*/
    getSpanArr(data) {
      if (data == null) {
        return;
      } else {
        /*对象名称集*/
        this.parameterNameArr = [];
        /*对象名称相同的个数*/
        this.parameterNamePop = 0;
      }
      for (let i = 0; i < data.length; i++) {
        if (i === 0) {
          /*对象名称集*/
          this.parameterNameArr.push(1);
          this.parameterNamePop = 0;
        } else {
          if (data[i].pyLevel === data[i - 1].pyLevel) {
            this.parameterNameArr[this.parameterNamePop] += 1;
            this.parameterNameArr.push(0);
          } else {
            this.parameterNameArr.push(1);
            this.parameterNamePop = i;
          }
        }
      }
    },
    //合并单元格
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const row_2 = this.parameterNameArr[rowIndex];
        const col_2 = row_2 > 0 ? 1 : 0;
        return {
          rowspan: row_2,
          colspan: col_2,
        };
      }
    },
  },
  mounted() {
    this.pykcList = this.$route.query.pykcList;
    console.log(this.pykcList, "pykcL =========");
    this.queryInfo();
  },
};
</script>

<style lang="less" scoped>
@import "../../../../styles/delayedExam.less";
@import "../../../../styles/supervision.less";
.pagination-block {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: center;
}

/deep/ .header-row-class {
  .header-cell-class {
    border-bottom: 1px solid #c7d7eb;
  }
}

/deep/ .table-container {
  .el-table td,
  .el-table th.is-leaf,
  .el-table--border,
  .el-table--group {
    border-color: #c7d7eb;
  }

  .el-table__header tr,
  .el-table__header th {
    padding: 0;
    height: 76px;
    background-color: #edf2f9;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    line-height: 20px;
    color: #004ca7;
  }

  .el-table__body tr,
  .el-table__body td {
    padding: 0;
    height: 86px;
  }

  .wrapper-button {
    display: flex;
    flex-direction: column;
    align-items: center;

    > div {
      margin-bottom: 8px;
    }
  }
  .table-data {
    width: 100%;
    border: 0.8px solid #004ca7;
    border-radius: 9px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #004ca7;
  }
}

/deep/ .el-dialog {
  height: 320px;
}

.assessment-container {
  width: calc(100% - 20px);
  .table-container {
     width: 100%;
  }
}

.bottom-block{
  width: calc(100% - 20px);
  margin: 0 auto;
}

.class-tip {
  color: #004ca7;
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  margin: 40px;
}
</style>


<style lang="less" scoped>
  #plan-import-confirm{
    /deep/ .el-button--warning.is-plain{
      background: #FFF6EF;
      border-color: #F3B815;
      color: #F3B815;
      border-radius: 6px;
    }
    /deep/ .el-button--warning.is-plain:focus, .el-button--warning.is-plain:hover{
      color: #F3B815;
      background: #FFF6EF;
      border: 1px solid #F3B815;
      border-radius: 6px;
    }

    /deep/ .el-button--danger.is-plain{
      background: #FFEFEF;
      border-color: #FF3819;
      color: #FF3819;
      border-radius: 6px;
    }

    /deep/ .el-button--danger.is-plain:hover{
      background: #FFEFEF;
      border: 1px solid #FF3819;
      color: #FF3819;
      border-radius: 6px;
    }
  }
</style>
